const mongoose = require('mongoose');
const moment = require('moment/moment');
moment.locale('zh-cn');

// 定义书籍的Schema
const BookSchema = new mongoose.Schema({
    name: {
        type: String,
        required: [true, '书名必须存在'],
        trim: true,  // 去除首尾空格
    },
    author: {
        type: String,
        required: [true, '作者必须存在'],
        trim: true,
    },
    borrowName: String,
    content: String,
    categoryId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Categories',  // 关联Category模型，表明这个字段引用自Category模型的_id
        required: [true, '分类ID必须填写。'],
    },
    // 假设你的系统中有对应的User模型，这里关联用户
    userId: {
        type: String,
    },
    createdAt: {
        type: Date,
        get: function () {
            return moment(this.createdAt).format("LL");
        }
    },
    updatedAt: {
        type: Date,
        get: function () {
            return moment(this.updatedAt).format("LL");
        }
    },
    picture: {
        type: String,
        required: [true, '图片字段必须存在']
    },
    likesCount: {
        type: Number
    },
    likeUsers: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }
    // borrowBook: {
    //     type: Boolean,
    //     required: [true, '借阅状态必须存在'],
    // }
});

// 定义虚拟属性（如果有需要，比如关联的一些操作可以在这里补充更多虚拟属性相关逻辑）

// 创建Book模型
const Book = mongoose.model('Book', BookSchema);

module.exports = Book;